const fs = window.require('fs');

export function generate_date_str(date, type) {
    let year = date.getFullYear();
    let month = (date.getMonth() < 9) ? ('0' + (date.getMonth() + 1)) : ((date.getMonth() + 1).toString());
    let day = (date.getDate() < 10) ? ('0' + date.getDate()) : (date.getDate().toString());
    let hour = (date.getHours() < 10) ? ('0' + date.getHours()) : (date.getHours().toString());
    let min = (date.getMinutes() < 10) ? ('0' + date.getMinutes()) : (date.getMinutes().toString());
    let sec = (date.getSeconds() < 10) ? ('0' + date.getSeconds()) : (date.getSeconds().toString());

    let data_str = '';
    if (type === 'file') {
        data_str = `${year}${month}${day}_${hour}${min}${sec}`;
    }

    if (type === 'show') {
        data_str = `${year}-${month}-${day} ${hour}:${min}`;
    }
    return data_str;
}

export function rmdir_recursive(path) {
    fs.readdirSync(path).forEach((file, index) => {
        const cur_path = path + "/" + file;

        if (fs.lstatSync(cur_path).isDirectory()) {
            if (rmdir_recursive(cur_path) < 0)
                return -1;
        } else {
            try {
                fs.unlinkSync(cur_path);
            } catch (err) {
                return -1;
            }
        }
    });

    try {
        fs.rmdirSync(path);
        return 0;
    } catch (err) {
        return -1;
    }
}

export function block_sleep(delay_ms) {
    var start = (new Date()).getTime();
    while ((new Date()).getTime() - start < delay_ms) {
        continue;
    }
}
